﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class PurchasePlanRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15141;
        protected GridView gvPurchasePlanRpt;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblInvQty;
        protected Label lblInvQtyText;
        protected Label lblMsg;
        protected Label lblOrderedQty;
        protected Label lblOrderedQtyText;
        protected Label lblReceiptInquiryList;
        protected Label lblTransitQty;
        protected Label lblTransitQtyText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalInvQty;
        private decimal TotalOrderQty;
        private decimal TotalTransitQty;
        protected UpdatePanel up1;

        public void BindData2StockList(PurchasePlanRptQueryEntity purchasePlanRptQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(purchasePlanRptQuery.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetPurchasePlanRptDataSet(purchasePlanRptQuery);
            base.BindData2GridView(this.gvPurchasePlanRpt, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            PurchasePlanRptQueryEntity purchasePlanRptQuery = this.GetPurchasePlanRptQueryEntity();
            purchasePlanRptQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = purchasePlanRptQuery.CurrentPage;
            purchasePlanRptQuery.CurrentPage = "0";
            this.gvPurchasePlanRpt.Columns[1].Visible = false;
            this.gvPurchasePlanRpt.AllowSorting = false;
            this.BindData2StockList(purchasePlanRptQuery);
            this.gvPurchasePlanRpt.ShowFooter = false;
            purchasePlanRptQuery.IsGetAll = false;
            purchasePlanRptQuery.CurrentPage = currentPageIndex;
            this.SetPurchasePlanRptQueryEntity(purchasePlanRptQuery);
            base.Export2Excel("PurchasePlanRpt.xls", this.gvPurchasePlanRpt);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PurchasePlanRptQueryEntity purchasePlanRptQuery = this.GetPurchasePlanRptQueryEntity();
            purchasePlanRptQuery.CurrentPage = "0";
            this.BindData2StockList(purchasePlanRptQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PurchasePlanRptQueryEntity purchasePlanRptQuery = this.GetPurchasePlanRptQueryEntity();
            purchasePlanRptQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2StockList(purchasePlanRptQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            PurchasePlanRptQueryEntity purchasePlanRptQuery = this.GetPurchasePlanRptQueryEntity();
            purchasePlanRptQuery.OWNER_ID = "";
            purchasePlanRptQuery.SKU_ID = "";
            purchasePlanRptQuery.SKU_CLASS_ID = "";
            this.BindData2StockList(purchasePlanRptQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PurchasePlanRptQueryEntity purchasePlanRptQuery = this.GetPurchasePlanRptQueryEntity();
            if (int.Parse(purchasePlanRptQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                purchasePlanRptQuery.CurrentPage = (int.Parse(purchasePlanRptQuery.CurrentPage) + 1).ToString();
                this.BindData2StockList(purchasePlanRptQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            PurchasePlanRptQueryEntity purchasePlanRptQuery = this.GetPurchasePlanRptQueryEntity();
            if (int.Parse(purchasePlanRptQuery.CurrentPage) > 0)
            {
                purchasePlanRptQuery.CurrentPage = (int.Parse(purchasePlanRptQuery.CurrentPage) - 1).ToString();
                this.BindData2StockList(purchasePlanRptQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            PurchasePlanRptQueryEntity purchasePlanRptQuery = this.GetPurchasePlanRptQueryEntity();
            this.BindData2StockList(purchasePlanRptQuery);
        }

        private PurchasePlanRptQueryEntity GetPurchasePlanRptQueryEntity()
        {
            return (base.PageQueryEntitySession as PurchasePlanRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                PurchasePlanRptQueryEntity purchasePlanRptQuery = this.GetPurchasePlanRptQueryEntity();
                purchasePlanRptQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2StockList(purchasePlanRptQuery);
            }
        }

        protected void gvPurchasePlanRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15241", this.gvPurchasePlanRpt);
        }

        protected void gvPurchasePlanRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                decimal invQty = 0M;
                if (((Label) e.Row.FindControl("lblInvQty")) != null)
                {
                    this.TotalInvQty += decimal.Parse((((Label) e.Row.FindControl("lblInvQty")).Text != "") ? ((Label) e.Row.FindControl("lblInvQty")).Text : "0");
                    invQty = this.TotalInvQty;
                }
                decimal TransitQty = 0M;
                if (((Label) e.Row.FindControl("lblTransitQty")) != null)
                {
                    this.TotalTransitQty += decimal.Parse((((Label) e.Row.FindControl("lblTransitQty")).Text != "") ? ((Label) e.Row.FindControl("lblTransitQty")).Text : "0");
                    TransitQty = this.TotalTransitQty;
                }
                decimal MinQty = 0M;
                if (((Label) e.Row.FindControl("lblMinQty")) != null)
                {
                    this.TotalTransitQty += decimal.Parse((((Label) e.Row.FindControl("lblMinQty")).Text != "") ? ((Label) e.Row.FindControl("lblMinQty")).Text : "0");
                    MinQty = this.TotalTransitQty;
                }
                decimal ReOrderQty = 0M;
                if (!string.IsNullOrEmpty(((Label) e.Row.FindControl("lblReorderPoint")).Text))
                {
                    ReOrderQty = decimal.Parse(((Label) e.Row.FindControl("lblReorderPoint")).Text);
                    decimal Qty = (invQty + TransitQty) - MinQty;
                    if (Qty < 0M)
                    {
                        Qty *= -1M;
                    }
                    if (Qty >= ReOrderQty)
                    {
                        ((Label) e.Row.FindControl("lblOrderedQty")).Text = base.GetPreferenceDigit(Qty);
                        this.TotalOrderQty += Qty;
                    }
                    else
                    {
                        ((Label) e.Row.FindControl("lblOrderedQty")).Text = base.GetPreferenceDigit(ReOrderQty);
                        this.TotalOrderQty += ReOrderQty;
                    }
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblInvQtyText.Text = base.GetPreferenceDigit(this.TotalInvQty);
                this.lblOrderedQtyText.Text = base.GetPreferenceDigit(this.TotalOrderQty);
                this.lblTransitQtyText.Text = base.GetPreferenceDigit(this.TotalTransitQty);
            }
        }

        protected void gvPurchasePlanRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PurchasePlanRptQueryEntity purchasePlanRptQuery = this.GetPurchasePlanRptQueryEntity();
                purchasePlanRptQuery.SortField = e.SortExpression;
                if (purchasePlanRptQuery.SortDirection == "ASC")
                {
                    purchasePlanRptQuery.SortDirection = "DESC";
                }
                else if (purchasePlanRptQuery.SortDirection == "DESC")
                {
                    purchasePlanRptQuery.SortDirection = "ASC";
                }
                this.BindData2StockList(purchasePlanRptQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            PurchasePlanRptQueryEntity purchasePlanRptQuery = new PurchasePlanRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    purchasePlanRptQuery.OWNER_ID = this.PreviousPage.OwnerID;
                    purchasePlanRptQuery.SKU_ID = this.PreviousPage.SkuID;
                    purchasePlanRptQuery.SKU_CLASS_ID = this.PreviousPage.SkuClassID;
                }
                else
                {
                    purchasePlanRptQuery.OWNER_ID = "";
                    purchasePlanRptQuery.SKU_ID = "";
                    purchasePlanRptQuery.SKU_CLASS_ID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            purchasePlanRptQuery.IsGetAll = false;
            purchasePlanRptQuery.CurrentPage = "0";
            purchasePlanRptQuery.PageSize = this.gvPurchasePlanRpt.PageSize.ToString();
            purchasePlanRptQuery.SortField = "INV.OWNER_ID,INV.SKU_ID";
            purchasePlanRptQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvPurchasePlanRpt.PageSize;
            this.SetPurchasePlanRptQueryEntity(purchasePlanRptQuery);
            this.BindData2StockList(purchasePlanRptQuery);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvPurchasePlanRpt);
                this.LoadData();
            }
        }

        public void SetPurchasePlanRptQueryEntity(PurchasePlanRptQueryEntity purchasePlanRptQuery)
        {
            base.PageQueryEntitySession = purchasePlanRptQuery;
        }

        public PurchasePlanRptQuery PreviousPage
        {
            get
            {
                return (PurchasePlanRptQuery) base.PreviousPage;
            }
        }
    }
}

